package com.cloudOffice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cloudOffice.constant.HttpConstant;
import com.cloudOffice.constant.SystemConstant;
import com.cloudOffice.execption.CustomException;
import com.cloudOffice.execption.SystemException;
import com.cloudOffice.model.system.SysRole;
import com.cloudOffice.result.Result;
import com.cloudOffice.service.SysRoleService;
import com.cloudOffice.vo.system.AssignRoleVo;
import com.cloudOffice.vo.system.SysRoleQueryVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @program: cloud-office
 * @className: SysRoleController
 * @description: 角色管理
 * @data: 2024/2/4 15:45
 * @author: ihu
 * @version: 1.0
 **/

@Slf4j
@Tag(name = "角色管理")
@RestController("sysRoleController")
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {
	
	@Resource
	private SysRoleService sysRoleService;
	
	
	/**
	 * 查询所有角色
	 */
	@GetMapping("findAll")
	@Operation(summary = "查询所有角色")
	public Result<List<SysRole>> findAll() {
		log.info("查询所有角色");
		return Result.success(sysRoleService.list());
	}
	
	/**
	 * 分页查询
	 */
	@Operation(summary = "条件分页查询")
	@GetMapping("{page}/{limit}")
	public Result<IPage<SysRole>> pageQueryRole(@PathVariable Long page,
	                                            @PathVariable Long limit,
	                                            SysRoleQueryVo sysRoleQueryVo) {
		log.info("条件分页查询：page: {}, limit: {}, sysRoleQueryVo: {}", page, limit, sysRoleQueryVo);
		return sysRoleService.pageQueryRole(page, limit, sysRoleQueryVo);
	}
	
	/**
	 * 根据id查询角色
	 */
	@Operation(summary = "根据id查询角色")
	@GetMapping("get/{id}")
	public Result<SysRole> get(@PathVariable Long id) {
		log.info("根据id查询角色：id: {}", id);
		return Result.success(sysRoleService.getById(id));
	}
	
	/**
	 * 新增角色
	 */
	@PostMapping("save")
	@Operation(summary = "新增角色")
	public Result<?> save(@RequestBody @Validated SysRole role) {
		log.info("新增角色：role: {}", role);
		isExistRoleCode(role);
		sysRoleService.save(role);
		return Result.success();
	}
	
	/**
	 * 修改角色
	 */
	@Operation(summary = "修改角色")
	@PutMapping("update")
	public Result<?> updateById(@RequestBody SysRole role) {
		log.info("修改角色：role: {}", role);
		isExistRoleCode(role);
		sysRoleService.updateById(role);
		return Result.success();
	}
	
	/**
	 * 删除角色
	 */
	@Operation(summary = "删除角色")
	@DeleteMapping("remove/{id}")
	public Result<?> remove(@PathVariable Long id) {
		log.info("删除角色：id: {}", id);
		if (sysRoleService.getById(id) == null)
			throw new CustomException(HttpConstant.CODE_404, SystemConstant.SYSROLE_ID_NOT_EXIST);
		sysRoleService.removeById(id);
		return Result.success();
	}
	
	/**
	 * 批量删除角色
	 */
	@Operation(summary = "根据id批量删除角色")
	@DeleteMapping("batchRemove")
	public Result<?> batchRemove(@RequestBody List<Long> idList) {
		log.info("根据id批量删除角色：idList: {}", idList);
		idList.forEach(e -> {
			if (sysRoleService.getById(e) == null)
				throw new CustomException(HttpConstant.CODE_404, e + SystemConstant.SYSROLE_ID_NOT_EXIST);
		});
		sysRoleService.removeByIds(idList);
		return Result.success();
	}
	
	/**
	 * 根据角色Id获取角色
	 */
	@GetMapping("/toAssign/{userId}")
	@Operation(summary = "根据用户Id获取角色")
	public Result<Map<String, Object>> toAssign(@PathVariable Long userId) {
		log.info("根据用户Id获取角色：userId: {}", userId);
		return Result.success(sysRoleService.findRoleByAdminId(userId));
	}
	
	/**
	 * 根据用户Id分配角色
	 */
	@PostMapping("/doAssign")
	@Operation(summary = "根据用户Id分配角色")
	public Result<?> doAssign(@RequestBody AssignRoleVo assignRoleVo) {
		log.info("根据用户Id分配角色：assignRoleVo: {}", assignRoleVo);
		sysRoleService.doAssign(assignRoleVo);
		return Result.success();
	}
	
	/**
	 * 判断角色编码是否存在
	 */
	private void isExistRoleCode(SysRole role) {
		if (sysRoleService.getOne(Wrappers.<SysRole>lambdaQuery().eq(SysRole::getRoleCode, role.getRoleCode())) != null)
			throw new SystemException(HttpConstant.CODE_400, SystemConstant.SYSROLE_CODE_EXISTS);
	}
	
	
}
